//
//  ViewController.m
//  25-多线程
//
//  Created by GivenBmli on 16/8/17.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *ImageViewThread;

@end

@implementation ViewController
// http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//      iOS 下的多线程
//      1.进程和线程
//      什么是进程?
//      进程是当用户下达执行应用程序的命令是,所启动的东西 进程比作工厂
//      什么是线程?
//      线程是进程的执行单位,线程比做工人
//      2.多线程
//      为了更好的进行一些程序的操作,提升效率.
//      一个进程不能过多的创建线程,资源消耗过大(工厂不能有太多工人,工资开不起)开辟5个左右的线程
//      注意:所有UI的操作,都放在主线程中进行
//      需要开辟线程的耗时操作图像文字渲染,大数据存储,网络请求等.
//    
//      3.NSThread
//      获取到当前线程<NSThread: 0x7fc10bd03730>{number = 1, name = main}
//      name 线程名 number线程编号
//      main 代表主线程
    
    
  NSThread *thread = [NSThread currentThread ];
    NSLog(@"%@", thread);
//    2.>开辟子线程执行操作
//    子线程随机创建
    [NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil];
/**
    [NSThread detachNewThreadWithBlock:^{
    NSThread *thread = [NSThread currentThread];
    NSLog(@"newThread:%@", thread);
     }];
*/
   //     3>延迟当前线程执行,固定时间
//    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
//    [NSThread sleepUntilDate:date];
//    NSLog(@"1111");
//    4>延迟当前线程执行,相对时间
//    [NSThread sleepForTimeInterval:1];
//        NSLog(@"1111");
//    5>exit 退出当前线程 ,不要退出主线程
//    退出后,线程后续操作不会执行
//    [NSThread exit];
//    6.>threadPriority 线程优先级
//    默认线程优先级0.5
   double priority = [NSThread threadPriority];
    NSLog(@"%lf",priority);
//    7> name 线程名
    [NSThread mainThread].name = @"xxx的主线程";
    NSLog(@"%@",[NSThread mainThread]);
//    8>isMainThread 判断是否是主线程
//    判断当前线程是不是主线程
    [NSThread isMainThread];
//    判断线程对象是不是主线程
    [[NSThread mainThread]isMainThread];
//    9> 初始化方法 需要手动管理线程的生命周期
//    初始化一个未启动的线程
    NSThread *newTherad = [[NSThread alloc]initWithTarget:self selector:@selector(myTherad) object:nil];
     [newTherad start];
//    练习2.
//    [NSThread detachNewThreadSelector:@selector(ImageViewDerr) toTarget:self withObject:nil];
//    10> 将耗时的放入后台执行
//    不关心线程问题
    [self performSelectorInBackground:@selector(ImageViewDerr) withObject:nil];
//    11>performSelectorOnMainThread返回主线程执行操作

   
}
// 数据网址不能有空格

- (void)ImageViewDerr {
    NSData *date = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"]];
//    返回主线程,显示图片
    [self performSelectorOnMainThread:@selector(onMainThreadHandelImage:) withObject:date waitUntilDone:YES];
//    _ImageViewThread.image = [UIImage imageWithData:date];
    
}
- (void)onMainThreadHandelImage:(id)object {
    _ImageViewThread.image = [UIImage imageWithData:object];
}

-(void)myTherad {
    for (int i = 0; i< 1000000; i++) {
        if (i == 99999) {
            [NSThread exit];
        }
    }

    
    NSLog(@"%s",__FUNCTION__);
}
- (void)newThread {
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
    [NSThread sleepUntilDate:date];
    NSThread *thread = [NSThread currentThread];
    thread.name = @"王道林的线程";
    double priority = [NSThread threadPriority];
    NSLog(@"2222%f",priority);
    [self performSelectorInBackground:@selector(myTherad) withObject:nil];

    NSLog(@"newThread:%@",thread);
}
- (IBAction)clicked:(id)sender {
    for (int i = 0; i< 1000000; i++) {
        NSLog(@"循环了:%d次",i);
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
